import { performance } from 'perf_hooks';
import {buildTx, ExchangeCard, sendTX, TransferBcosERC20} from './client/bcos/BcosClient.js';


const concurrency = 100; // 并发数
const iterations = 20; // 测试次数

let completedRequests = 0;
let totalRequestTime = 0;


const signedTxDataArray = [];

// 循环生成签名
async function generateSignedTxData() {
  
  for (let i = 0; i < concurrency * iterations; i++) {
    try {
      const privateKey = '0x3f560cb18f11f5946f1ba2e09a3feda9fbd33d364bab5fb2f170e8c9d5e3a5f0'; // 设置参数
      const assetAddress = '0x30610fc6d2cdb887d2e70d0371218081348ee0ef'; // 设置参数
      const toAddress = '0xE8Ed36E87453A8751Fd91c788f5C81aE729DA370'; // 设置参数
      const amount = "1"; // 设置参数
      const asset = {"assetConfig": {"chainID": "wbbcoccnode", "groupID": "group0", "rechargeWithdrawContractAddress": "0x028d27e08e09a5f612c90a84967189c7bcf2c4b4"}, "assetIcon": "", "assetName": "元豆", "assetStatus": 1, "assetSymbol": "yuandou", "assetType": "20", "contractAddress": "0x30610fc6d2cdb887d2e70d0371218081348ee0ef", "networkName": "main"}; // 设置参数
      const signedTxData = await buildTx(privateKey, assetAddress, toAddress, amount, asset);
      signedTxDataArray.push(signedTxData);
      console.log(`Signed transaction data ${i + 1} generated successfully.`);
    } catch (error) {
      console.error(`Error generating signed transaction data ${i + 1}:`, error);
    }
  }
}


async function runConcurrentTransferRequests() {
  const promises = [];
  for (let i = 0; i < concurrency; i++) {
    promises.push(runSingleTransferRequest(i));
  }
  await Promise.all(promises);
}

async function runSingleTransferRequest(index) {
  for (let i = index * iterations  ; i < index * iterations + iterations; i++) {
    const fromAddress = '0x3C7cf2F5424de476E6B4fE49aBcc5ba8185884c6'; // 设置参数
    const asset = {"assetConfig": {"chainID": "wbbcoccnode", "groupID": "group0", "rechargeWithdrawContractAddress": "0x028d27e08e09a5f612c90a84967189c7bcf2c4b4"}, "assetIcon": "", "assetName": "元豆", "assetStatus": 1, "assetSymbol": "yuandou", "assetType": "20", "contractAddress": "0x30610fc6d2cdb887d2e70d0371218081348ee0ef", "networkName": "main"}; // 设置参数
    try {
      const signedTxData = signedTxDataArray[i];
      // 调用要测试的方法
      const startRequestTime = performance.now();
      const result = await sendTX(fromAddress, signedTxData, asset);
      const endRequestTime = performance.now();
      const requestTime = endRequestTime - startRequestTime;
      console.log("requestTime = ", requestTime)
      totalRequestTime += requestTime;
      console.log(`Request ${index + 1} - Iteration ${i + 1} successful. Result:`, result);
      completedRequests++;
    } catch (error) {
      console.error(`Request ${index + 1} - Iteration ${i + 1} failed:`, error);
    }
  }
}


generateSignedTxData().then(() => {
    console.log('All signed transaction data generated.');
  
    const startTime = performance.now();
    runConcurrentTransferRequests().then(() => {
      const endTime = performance.now();
      const totalTime = endTime - startTime;
      const tps = completedRequests / (totalTime / 1000);
      const averageRequestTime = totalRequestTime / completedRequests;
      console.log(`All requests completed in ${totalTime} milliseconds.`);
      console.log(`Average request time: ${averageRequestTime} milliseconds.`);
      console.log(`TPS: ${tps}`);
    });
    // 这里可以开始压测或者进行其他操作
  }).catch(error => {
    console.error('Error generating signed transaction data:', error);
  });



